<template>
    <div class="login_container">
        <div class="login_box">

            <div class="logo">
                <img src="../assets/touxiang.jpg" alt="">
            </div>
            <!-- 登录框界面 -->
            <el-form ref="loginRef" :model="loginForm" :rules="loginRules" label-width="0px" class="login2">
                <!-- 登录表单 -->
                <el-form-item prop="username">
                    <el-input v-model="loginForm.username" prefix-icon=" el-icon-search"></el-input>
                </el-form-item>
                <el-form-item prop="password">
                    <el-input v-model="loginForm.password" type="password" prefix-icon="el-icon-search"></el-input>
                </el-form-item>
                <el-form-item class="btns">
                    <el-button type="primary" plain @click="val">登录</el-button>
                    <el-button type="info" plain @click="rest">重置</el-button>
                </el-form-item>
            </el-form>
        </div>



    </div>
</template>

<script>
export default {
    name: 'WorkspaceJsonLogin',

    data () {
        return {
            //登录表单的数据绑定
            loginForm: {
                username: 'admin',
                password: '123456',
            },
            //登录表单的验证规则
            loginRules: {
                //验证用户名
                username: [{
                    required: true, message: '请输入用户名', trigger: 'blur'
                },
                { min: 3, max: 11, message: '长度在5-11个字符之间', trigger: 'blur' }
                ],
                //验证密码
                password: [{ required: true, message: '请输入密码', trigger: 'blur' },
                { min: 6, max: 10, message: '长度在6-10个字符之间', trigger: 'blur' }
                ]
            },
        };
    },

    mounted () {

    },

    methods: {
        rest () {
            this.$refs.loginRef.resetFields();//重置表单
        },
        val () {
            // console.log(this.$refs.loginRef)
            this.$refs.loginRef.validate(async valid => {
                // console.log(valid);
                if (!valid) return;
                const { data: res } = await this.$http.post('login', this.loginForm)
                if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
                this.$message.success(res.meta.msg)
                //登录成功后的token保存到客户端的sessionStorage中
                window.sessionStorage.setItem('token', res.data.token)
                //通过编程式导航跳转到后台主页,路由地址是'/home'
                this.$router.push('/home')
            })
        },
    },
};
</script>

<style lang="less" scoped>
.login_container {
    background-color: #c6c9cc;
    height: 100%;


}

.login_box {
    width: 450px;
    height: 300px;
    border-radius: 3px;
    background-color: #bebec1;
    // background: linear-gradient(#43453a);
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);

    .logo {
        width: 130px;
        height: 130px;
        border: solid 1px #ccc;
        border-radius: 50%;
        padding: 6px;
        box-shadow: 0 0 10px #333;
        position: absolute;
        left: 50%;
        transform: translate(-50%, -50%);


        img {
            width: 100%;
            height: 100%;
            border-radius: 50%;
        }


    }
}

.login2 {
    position: absolute;
    bottom: 0;
    width: 100%;
    padding: 0 30px;
    box-sizing: border-box;

    .btns {
        display: flex;
        justify-content: flex-end
    }
}
</style>

